package com.example.a30;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class EntertainmentExpenseActivity extends AppCompatActivity {

    private DBHelper dbHelper;
    private EditText amountEditText;
    private Button addExpenseButton;
    private TextView totalExpenseTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entertainment_expense);

        dbHelper = new DBHelper(this);
        amountEditText = findViewById(R.id.amountEditText);
        addExpenseButton = findViewById(R.id.addExpenseButton);
        totalExpenseTextView = findViewById(R.id.totalExpenseTextView);

        addExpenseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double amount = Double.parseDouble(amountEditText.getText().toString());
                String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
                dbHelper.addExpense(amount, date, "Entertainment");
                updateTotalExpense();
                Toast.makeText(EntertainmentExpenseActivity.this, "Expense added successfully", Toast.LENGTH_SHORT).show();
            }
        });

        updateTotalExpense();
    }

    private void updateTotalExpense() {
        double totalExpense = dbHelper.getTotalEntertainmentExpense();
        totalExpenseTextView.setText("娱乐支出总计： " + totalExpense);
    }
}
